package com.liming.user.controller;

import com.liming.auth.client.UserAuthClient;
import com.liming.common.pojo.dto.authinfo.AddOrUpdateUserAddressDTO;
import com.liming.common.pojo.dto.authinfo.UsernameLoginDTO;
import com.liming.common.pojo.dto.authinfo.mini.UpdateUserInfoDTO;
import com.liming.common.pojo.dto.authinfo.mini.UpdateUserPwdDTO;
import com.liming.common.pojo.dto.coupon.UserHomeCouponPageQuery;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.authinfo.CaptchaVO;
import com.liming.common.pojo.vo.authinfo.mini.MiniAddressVO;
import com.liming.common.pojo.vo.authinfo.mini.MiniRegionVO;
import com.liming.common.pojo.vo.authinfo.mini.MiniUserHomeVO;
import com.liming.common.pojo.vo.authinfo.mini.MiniUserInfoVO;
import com.liming.common.pojo.vo.coupon.mini.MiniUserHomeCouponVO;
import com.liming.common.result.LMResult;
import com.liming.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author liming
 * @date 2025/1/2
 */
@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "用户信息接口-Web")
@RequestMapping("/user/info")
public class UserInfoController {

    private final UserAuthClient userAuthClient;
    private final UserInfoService userInfoService;


    @Operation(summary = "登录 - 微信小程序一键登录")
    @GetMapping("login_wx_mini")
    public LMResult<String> loginWithWxMini(@RequestParam String code) {
        return LMResult.success(userAuthClient.loginWithWxMini(code));
    }

    @Operation(
            summary = "列表 - 用户收货地址",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = MiniAddressVO.class))
                    })
            }
    )
    @GetMapping("address")
    public LMResult<List<MiniAddressVO>> addressList() {
        return LMResult.success(userAuthClient.addressList());
    }

    @Operation(
            summary = "信息 - 用户主页",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = MiniUserHomeVO.class))
                    })
            }
    )
    @GetMapping("home")
    public LMResult<MiniUserHomeVO> home() {
        return LMResult.success(userInfoService.miniUserHome());
    }

    @Operation(
            summary = "分页 - 用户优惠券分页",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = MiniUserHomeCouponVO.class))
                    })
            }
    )
    @GetMapping("homeCoupon")
    public LMResult<PageVO<MiniUserHomeCouponVO>> homeCoupon(UserHomeCouponPageQuery pageQuery) {
        return LMResult.success(userInfoService.miniUserHomeCoupon(pageQuery));
    }

    @Operation(
            summary = "详情 - 个人信息",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = MiniUserInfoVO.class))
                    })
            }
    )
    @GetMapping("info")
    public LMResult<MiniUserInfoVO> info() {
        return LMResult.success(userAuthClient.info());
    }

    @Operation(
            summary = "登录 - 用户名",
            responses = {
                    @ApiResponse(responseCode = "1", description = "登录成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = String.class))
                    })
            }
    )
    @PostMapping("login_username")
    public LMResult<String> loginWithUsername(@RequestBody UsernameLoginDTO usernameLoginDTO) {
        String result = userAuthClient.loginWithUsername(usernameLoginDTO);
        return LMResult.success(result);
    }

    @Operation(summary = "新增|更新 - 用户收货地址")
    @PostMapping("address")
    public LMResult<Boolean> addAddress(@RequestBody AddOrUpdateUserAddressDTO addOrUpdateUserAddressDTO) {
        return LMResult.success(userAuthClient.addOrUpdateAddress(addOrUpdateUserAddressDTO));
    }

    @Operation(summary = "退出登录")
    @PostMapping("logout")
    public LMResult<Boolean> logout() {
        Boolean flag = userAuthClient.logout();
        return LMResult.success(flag);
    }

    @Operation(
            summary = "登录验证码 - 线性干扰",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = CaptchaVO.class))
                    })
            }
    )
    @GetMapping("captcha_line")
    public LMResult<CaptchaVO> lineCaptcha() {
        return LMResult.success(userAuthClient.lineCaptcha());
    }

    @Operation(summary = "删除 - 用户收货地址")
    @DeleteMapping("address/{id}")
    public LMResult<Boolean> deleteAddress(@PathVariable Long id) {
        return LMResult.success(userAuthClient.deleteAddress(id));
    }

    @Operation(
            summary = "Map - 返回 ParentId - [{Id, name}]",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = Map.class))
                    })
            }
    )
    @GetMapping("region_map")
    public LMResult<Map<String, List<MiniRegionVO>>> regionMap() {
        return LMResult.success(userAuthClient.miniRegionMap());
    }

    @Operation(summary = "更新 - 用户信息")
    @PutMapping("info")
    public LMResult<Boolean> updateInfo(@RequestBody UpdateUserInfoDTO updateUserInfoDTO) {
        return LMResult.success(userAuthClient.updateInfo(updateUserInfoDTO));
    }

    @Operation(summary = "更新 - 用户密码")
    @PutMapping("password")
    public LMResult<Boolean> updatePassword(@RequestBody UpdateUserPwdDTO updateUserPwdDTO) {
        return LMResult.success(userAuthClient.updatePassword(updateUserPwdDTO));
    }
}
